package com.projetohibernate;

import java.util.ArrayList;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;


public class HibernateUtil {

	private static SessionFactory sessionFactory;
	
	
	static {
		try {
			sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
		} catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	

	public static void shutdown() {
		getSessionFactory().close();
	}
	
	public static void createTables(ArrayList<Class> annotedClass){
		
		try {
			AnnotationConfiguration cfg = new AnnotationConfiguration();
			
			for (Class class1 : annotedClass) {
				cfg.addAnnotatedClass(class1);
			}
			
			cfg.configure("hibernate.cfg.xml");
			
			new SchemaExport(cfg).create(true, true);
		} catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		}
		
	}

}
